package com.example.administrator.hqq.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.support.v4.app.NotificationManagerCompat;

import com.blankj.utilcode.util.SPUtils;
import com.example.administrator.hqq.config.LoongsConfig;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/12/3 15:59
 * @remark:
 */
public class VoiceAndVIbratorUtils {
    private static Ringtone r;
    private static Vibrator vibrator;

    public static void playVoice(Context context) throws Exception {
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        boolean isOpened = manager.areNotificationsEnabled();
        if (!isOpened)
        {
            return;
        }
        AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);//获取当前手机模式
        switch (audio.getRingerMode()) {
            case AudioManager.RINGER_MODE_SILENT://静音
                //do sth
                break;
            case AudioManager.RINGER_MODE_NORMAL://响铃
                if (SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).getBoolean(LoongsConfig.MessageNotification.voice,true))
                    defaultCallMediaPlayer(context);
                if (SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).getBoolean(LoongsConfig.MessageNotification.vibrator,true))
                    playVibrator(context);
                    break;
            case AudioManager.RINGER_MODE_VIBRATE://震动
                if (SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).getBoolean(LoongsConfig.MessageNotification.vibrator,true))
                    playVibrator(context);
                //do sth
                break;
        }
    }

    /**
     * 响铃
     * @param context
     * @throws Exception
     */
    public static void defaultCallMediaPlayer(Context context) throws Exception {
        if (r==null)
        {
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            r = RingtoneManager.getRingtone(context, notification);
        }
        if (!r.isPlaying())
        {

            r.play();
        }
    }
    public static void playVibrator(Context context)throws Exception
    {
        vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        vibrator.vibrate( new long[]{0,500},-1);
    }

}
